﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sinacor.Infra.Service.Data;
using System.Data.Common;
using Sinacor.Infra.Service.Common;

namespace Sinacor.OMM.Configuration.Repository
{
    /// <summary>
    /// Read repository configuration
    /// </summary>
    public class RepositoryConfigurationReader : BaseResourceAccess
    {

        #region Constructor

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="companyId"></param>
        public RepositoryConfigurationReader(Int32 companyId)
            : base(companyId)
        {

        }

        #endregion

        #region Methods

        /// <summary>
        /// Read repository id configuration
        /// </summary>
        /// <param name="repositoryId"></param>
        /// <returns></returns>
        public OMM.Configuration.Repository.RepositoryConfiguration GetConfiguration(Int32 repositoryId)
        {
            //
            OMM.Configuration.Repository.RepositoryConfiguration repositoryConfiguration = null;
            String sqlCommand = String.Empty;

            //
            sqlCommand = @" SELECT COD_REPRIO,
                                   NOME_REPRIO,
                                   DESC_REPRIO,
                                   COD_TIPO_REPRIO 
                            FROM TBOREPRIO
                            WHERE COD_REPRIO = :ID ";

            using (DbCommand dbCommand = this.DataBase.GetSqlStringCommand(sqlCommand))
            {
                this.DataBase.AddInParameter(dbCommand, "ID", System.Data.DbType.Int32, repositoryId);

                repositoryConfiguration = this.FindOne<OMM.Configuration.Repository.RepositoryConfiguration>(dbCommand,
                    reader =>
                    {
                        OMM.Configuration.Repository.RepositoryConfiguration item = new RepositoryConfiguration();

                        item.Id = reader.GetValue<Int32>("COD_REPRIO");
                        item.Name = reader.GetValue<String>("NOME_REPRIO");
                        item.Description = reader.GetValue<String>("DESC_REPRIO");
                        item.Type = EnumConverter.Parse<RepositoryTypes>(reader.GetValue<String>("COD_TIPO_REPRIO"));

                        return item;
                    });
            }

            repositoryConfiguration.ConnectionInformation = this.GetConnectionInformation(repositoryId);

            return repositoryConfiguration;
        }

        /// <summary>
        /// Read repository connection information
        /// </summary>
        /// <param name="repositoryId"></param>
        /// <returns></returns>
        private Dictionary<String, String> GetConnectionInformation(Int32 repositoryId)
        {
            //
            String sqlCommand = String.Empty;
            Dictionary<String, String> connectionInformation = new Dictionary<String, String>();
            List<KeyValue> _temporaryConnectionInformation = null;

            //
            sqlCommand = @" SELECT NOME_PRDD,
                                   TXT_CNTD_PRDD
                            FROM TBOREPRIO_DET
                            WHERE COD_REPRIO = :ID ";

            using (DbCommand dbCommand = this.DataBase.GetSqlStringCommand(sqlCommand))
            {
                this.DataBase.AddInParameter(dbCommand, "ID", System.Data.DbType.Int32, repositoryId);

                _temporaryConnectionInformation = this.FindAll<KeyValue>(dbCommand,
                    reader =>
                    {
                        KeyValue item = new KeyValue();

                        item.Key = reader.GetValue<String>("NOME_PRDD");
                        item.Value = reader.GetValue<String>("TXT_CNTD_PRDD");

                        return item;
                    });
            }

            foreach (KeyValue temporaryItem in _temporaryConnectionInformation)
                connectionInformation.Add(temporaryItem.Key, temporaryItem.Value);

            return connectionInformation;
        }


        #endregion

    }
}
